import {
    AfterContentChecked, AfterViewChecked, AfterViewInit, Component, ElementRef, Input, OnChanges, OnDestroy, OnInit,
    ViewChild
} from '@angular/core';
import { ServerSocket } from '@core/services/ws.service';

@Component({
  selector: 'app-c-brain',
  templateUrl: './c-brain.component.html',
  styleUrls: ['./c-brain.component.less']
})
export class CBrainComponent implements OnInit, AfterViewChecked, AfterViewInit, OnDestroy, OnChanges, AfterContentChecked {

  @ViewChild('top') top: ElementRef;
  @ViewChild('center') center: ElementRef;
  @ViewChild('all') all: ElementRef;

  @ViewChild('answerListBox') answerListBox: ElementRef;
  @Input() data;
  @Input() socket;
  @Input() IPPORT;
  @Input() goldObj;

  // 测试用
  // time;

    _answerList = [];
    // 正式时为父组件socket接收数据 @Input()至此组件;
    @Input()
    set answerList(value) {
        this._answerList = value;
        this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
    }

    get answerList() {
        return this._answerList;
    }
  constructor(public ws: ServerSocket, public ref: ElementRef) { }

  ngOnInit() {
  }

    ngAfterViewChecked() {
        // console.log('_________***********');
        // console.log(this.goldObj);
        this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
    }


    ngAfterViewInit() {
      // if (!this.socket) {
      //     // 测试用
      //     this.time = setInterval(() => {
      //         this.answerList.push({
      //             answer: '回答',
      //             name: 'user'
      //         });
      //         console.log(this.answerListBox.nativeElement.offsetHeight);
      //         this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
      //     }, 5000);
      // }
  }

  ngOnDestroy() {
      // clearInterval(this.time);
  }

  ngAfterContentChecked() {
      this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
  }
  ngOnChanges() {
      this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
  }

}
